package com.qianfeng.zhangwei.xutils.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.zhangwei.xutils.R;
import com.qianfeng.zhangwei.xutils.adapter.HomeAdapter;
import com.qianfeng.zhangwei.xutils.bean.AppBean;
import com.qianfeng.zhangwei.xutils.bean.Banner;
import com.qianfeng.zhangwei.xutils.bean.User;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class BitmapFragment extends Fragment {
    private String url = "http://nirvana.fruitday.com/api";

    @ViewInject(R.id.fragment_bitmap_listview)
    private ListView listView;
    private HomeAdapter adapter;

    private List<Banner> banners = new ArrayList<>();


    BitmapUtils bitmapUtils;
    private HttpUtils httpUtils = new HttpUtils();

    public BitmapFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bitmapUtils = new BitmapUtils(getActivity());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bitmap, container, false);
        ViewUtils.inject(this, view);
        init();
        send();
        return view;
    }

    private void send() {
        User user = new User();
        user.setName("1504");
        //  上传对象
//        String jsonUser = JSONObject.toJSONString(user);
//        List<User> list = new ArrayList<>();
//        String jsonList = JSON.toJSONString(list);
//
        RequestParams requestParams = new RequestParams();
//
//        requestParams.addBodyParameter("user", jsonList);


        requestParams.addBodyParameter("sign", "4b5c5b09495f149956d8fd790acc7969");
        requestParams.addBodyParameter("timestamp", "1438611225");
        requestParams.addBodyParameter("platform", "ANDROID");
        requestParams.addBodyParameter("source", "app");
        requestParams.addBodyParameter("connect_id", "");
        requestParams.addBodyParameter("region_id", "143949");
        requestParams.addBodyParameter("service", "marketing.banner");
        requestParams.addBodyParameter("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        requestParams.addBodyParameter("channel", "qq");
        requestParams.addBodyParameter("version", "2.2.0");

        httpUtils.send(HttpRequest.HttpMethod.POST, url, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获得json数据
                String result = responseInfo.result;
                AppBean bean = JSONObject.parseObject(result, AppBean.class);

                if (bean != null) {
                    banners.addAll(bean.getBanner());
                    adapter.notifyDataSetChanged();
                }


                //解析数据
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void init() {
        adapter = new HomeAdapter(getActivity(), banners,bitmapUtils);
        listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
        listView.setAdapter(adapter);
    }


}
